package com.easemob.chatuidemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.fragment.CommitFragment;
import com.easemob.chatuidemo.json.Banners;
import com.easemob.chatuidemo.net.BitmapCache;

import java.util.List;

/**
 * Created by lanou3g on 15/9/10.
 */
public class CommitViewPagerAdapter extends PagerAdapter {
    Context context;
    List<Banners> banners;
    LayoutInflater inflater;
    RequestQueue mQueue;
    ImageLoader imageLoader;
    Banners b;

    public CommitViewPagerAdapter(List<Banners> banners, Context context) {
        this.banners = banners;
        this.context = context;
        mQueue = Volley.newRequestQueue(context.getApplicationContext());
        imageLoader = new ImageLoader(mQueue, new BitmapCache());
    }

    @Override
    public int getCount() {
        return banners.size();
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        b = banners.get(position);
        inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item_viewpager, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.img_item_viewpager);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CommitFragment.class);
                intent.putExtra("image_path", b.getImage_host() + "/" + b.getImage_path());
                context.startActivity(intent);
                Log.e("intent", banners.get(position).getImage_path());
            }
        });
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.drawable.double_doll, R.drawable.double_doll);
        //加载图片
        Log.e("image", b.getImage_host() + "/" + b.getImage_path());
        imageLoader.get("http://" + b.getImage_host() + "/" + b.getImage_path(), listener);
        container.addView(view);
        return view;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        position = position % banners.size();
    }
}
